

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="分享开发的趣事">
  <meta name="author" content="RoseC(陈智豪)">
  <meta name="keywords" content="RoseC的博客,陈智豪,RoseC">
  <meta name="baidu-site-verification" content="code-2LJIrA8S4z" />
  <title>centos部署node+mongodb环境 - RoseC的博客</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.5.3/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.1.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"rose-ccc.gitee.io","root":"/","version":"1.8.5","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"copy_btn":true,"image_zoom":{"enable":true},"lazyload":{"enable":true,"onlypost":false},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>RoseC</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/indexImg/index.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="centos部署node+mongodb环境">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-11-29 17:06" pubdate>
        2020年11月29日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      891 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      12
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">centos部署node+mongodb环境</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2021年2月4日 晚上
                
              </p>
            
            <div class="markdown-body">
              <p>最近阿里云服务器过期了，买了其他类型的ESC，所以重新部署了一次后台，再加上上一篇讲的比较初略，所以这次详细讲讲</p>
<p>这次安装的东西有以下：<br>1、node环境<br>2、mongodb数据库<br>3、用于命令行终端切换的screen</p>
<h2 id="node环境安装"><a href="#node环境安装" class="headerlink" title="node环境安装"></a>node环境安装</h2><p>首先，安装之前要选好位置，我这里是在默认目录/usr/</p>
<h3 id="下载node包"><a href="#下载node包" class="headerlink" title="下载node包"></a>下载node包</h3><pre><code class="hljs bash">$ wget https://npm.taobao.org/mirrors/node/v14.13.1/node-v14.13.1-linux-x64.tar.xz
<span class="hljs-comment">#（注意当前文件位置，可以通过 pwd 查看当前目录）</span></code></pre>
<h3 id="解压"><a href="#解压" class="headerlink" title="解压"></a>解压</h3><pre><code class="hljs bash"><span class="hljs-comment"># xz格式，先用xz解压</span>
$ xz -d node-v14.13.1-linux-x64.tar.xz
<span class="hljs-comment"># 再用tar解压</span>
$ tar xvf node-v14.13.1-linux-x64.tar</code></pre>
<h3 id="设置环境变量（设置后可直接使用node命令）"><a href="#设置环境变量（设置后可直接使用node命令）" class="headerlink" title="设置环境变量（设置后可直接使用node命令）"></a>设置环境变量（设置后可直接使用node命令）</h3><pre><code class="hljs bash">$ vim ~/.bashrc
<span class="hljs-comment"># 在文件末尾添加内容并保存</span>
$ <span class="hljs-built_in">export</span> PATH=/usr/<span class="hljs-built_in">local</span>/src/node-v14.13.1-linux-x64/bin:<span class="hljs-variable">$PATH</span>
<span class="hljs-comment">#（1.输入i进入编辑模式；2.点击esc退出编辑模式，输入“：wq”退出）注意文件位置</span></code></pre>
<h3 id="使配置生效"><a href="#使配置生效" class="headerlink" title="使配置生效"></a>使配置生效</h3><pre><code class="hljs bash">$ <span class="hljs-built_in">source</span> ~/.bashrc</code></pre>
<h3 id="查看安装版本"><a href="#查看安装版本" class="headerlink" title="查看安装版本"></a>查看安装版本</h3><pre><code class="hljs bash">$ node -v
<span class="hljs-comment"># 若显示v14.13.1则表示安装成功</span></code></pre>
<h2 id="mongodb数据库安装"><a href="#mongodb数据库安装" class="headerlink" title="mongodb数据库安装"></a>mongodb数据库安装</h2><p>和安装node一样，先找一个安装的目录，我这里还是在/usr/中</p>
<h3 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h3><pre><code class="hljs bash">$ <span class="hljs-built_in">cd</span> /usr
$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.0.tgz</code></pre>
<h3 id="解压、重命名"><a href="#解压、重命名" class="headerlink" title="解压、重命名"></a>解压、重命名</h3><pre><code class="hljs bash">$ tar zxvf mongodb-linux-x86_64-4.0.0.tgz
$ mv mongodb-linux-x86_64-4.0.0 mongodb</code></pre>
<h3 id="配置环境变量"><a href="#配置环境变量" class="headerlink" title="配置环境变量"></a>配置环境变量</h3><pre><code class="hljs bash">$ vim /etc/profile
<span class="hljs-comment">#Set Mongodb  添加</span>
<span class="hljs-built_in">export</span> PATH=/usr/mongodb/bin:<span class="hljs-variable">$PATH</span>
<span class="hljs-comment">#Set Mongodb over   添加结束</span>

<span class="hljs-built_in">export</span> PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL  
<span class="hljs-comment">#在这一行上面加内容</span></code></pre>
<h3 id="使环境变量生效"><a href="#使环境变量生效" class="headerlink" title="使环境变量生效"></a>使环境变量生效</h3><pre><code class="hljs bash">$ <span class="hljs-built_in">cd</span> ~
$ <span class="hljs-built_in">source</span> /etc/profile
<span class="hljs-comment"># 到这里我们就可以使用mongo了，但是真正使用还要进行一些配置</span></code></pre>
<h3 id="配置启动"><a href="#配置启动" class="headerlink" title="配置启动"></a>配置启动</h3><h3 id="创建数据库目录"><a href="#创建数据库目录" class="headerlink" title="创建数据库目录"></a>创建数据库目录</h3><pre><code class="hljs bash">$ <span class="hljs-built_in">cd</span> /usr/mongodb
$ touch mongodb.conf
$ mkdir db
$ mkdir <span class="hljs-built_in">log</span>
$ <span class="hljs-built_in">cd</span> <span class="hljs-built_in">log</span>
$ touch mongodb.log</code></pre>
<h3 id="修改mongodb配置文件"><a href="#修改mongodb配置文件" class="headerlink" title="修改mongodb配置文件"></a>修改mongodb配置文件</h3><pre><code class="hljs bash">$ vim /usr/mongodb/mongodb.conf
<span class="hljs-comment"># 添加以下内容</span>

port=27017 <span class="hljs-comment">#端口</span>
dbpath= /usr/mongodb/db <span class="hljs-comment">#数据库存文件存放目录</span>
logpath= /usr/mongodb/<span class="hljs-built_in">log</span>/mongodb.log <span class="hljs-comment">#日志文件存放路径</span>
logappend=<span class="hljs-literal">true</span> <span class="hljs-comment">#使用追加的方式写日志</span>
fork=<span class="hljs-literal">true</span> <span class="hljs-comment">#以守护进程的方式运行，创建服务器进程</span>
maxConns=100 <span class="hljs-comment">#最大同时连接数</span>
noauth=<span class="hljs-literal">true</span> <span class="hljs-comment">#不启用验证</span>
journal=<span class="hljs-literal">true</span> <span class="hljs-comment">#每次写入会记录一条操作日志（通过journal可以重新构造出写入的数据）。</span>
bind_ip = 0.0.0.0  <span class="hljs-comment">#这样就可外部访问了，例如从win10中去连虚拟机中的MongoDB</span></code></pre>
<h3 id="设置文件夹权限"><a href="#设置文件夹权限" class="headerlink" title="设置文件夹权限"></a>设置文件夹权限</h3><pre><code class="hljs bash">$ <span class="hljs-built_in">cd</span> /usr/mongodb
$ chmod 777 db
$ chmod 777 <span class="hljs-built_in">log</span></code></pre>
<h3 id="启动mongodb"><a href="#启动mongodb" class="headerlink" title="启动mongodb"></a>启动mongodb</h3><pre><code class="hljs bash">$ <span class="hljs-built_in">cd</span> ~
$ mongod --config /usr/mongodb/mongodb.conf
<span class="hljs-comment"># 此时我们就可以正常启动数据库了，我们查看一下是否启动成功</span>
netstat -apn  | grep 27017</code></pre>

<p><img src="/2020/11/29/centos%E9%83%A8%E7%BD%B2node-mongodb%E7%8E%AF%E5%A2%83/centos%E9%83%A8%E7%BD%B2node-mongodb%E7%8E%AF%E5%A2%83.png" srcset="/img/loading.gif"><br>可以发现运行起来了，但是当我们退出窗口或退出命令，数据库会自动关闭，这时候我们需要将数据库设置成服务进程进行持久化</p>
<h3 id="配置服务进程"><a href="#配置服务进程" class="headerlink" title="配置服务进程"></a>配置服务进程</h3><h3 id="在-lib-system-目录下新建mongodb-service文件"><a href="#在-lib-system-目录下新建mongodb-service文件" class="headerlink" title="在/lib/system/目录下新建mongodb.service文件"></a>在/lib/system/目录下新建mongodb.service文件</h3><pre><code class="hljs bash">$ vim mongodb.service</code></pre>
<h3 id="添加如下内容"><a href="#添加如下内容" class="headerlink" title="添加如下内容"></a>添加如下内容</h3><pre><code class="hljs bash">[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/mongodb/bin/mongod --config /usr/mongodb/mongodb.conf
ExecReload=/bin/<span class="hljs-built_in">kill</span> -s HUP <span class="hljs-variable">$MAINPID</span>
ExecStop=/usr/mongodb/bin/mongod --shutdown --config /usr/mongodb/mongodb.conf
PrivateTmp=<span class="hljs-literal">true</span>
[Install]
WantedBy=multi-user.targe</code></pre>
<h3 id="设置权限"><a href="#设置权限" class="headerlink" title="设置权限"></a>设置权限</h3><pre><code class="hljs bash">$ chmod 754 mongodb.service</code></pre>
<h2 id="screen安装"><a href="#screen安装" class="headerlink" title="screen安装"></a>screen安装</h2><h3 id="下载-1"><a href="#下载-1" class="headerlink" title="下载"></a>下载</h3><pre><code class="hljs bash">$ yum install -y screen</code></pre>
<h3 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h3><pre><code class="hljs bash">$ screen -ls <span class="hljs-comment"># 查看所有screen</span>
$ screen -S &lt;screen-name&gt; <span class="hljs-comment"># 创建screen,并命名 </span>
$ ctr + A, D <span class="hljs-comment"># 快捷键,退出当前screen</span>
$ screen -r &lt;screen-name&gt; <span class="hljs-comment"># 进入screen </span>
$ screen -X quit <span class="hljs-comment"># 删除screen,但没有指定会话 </span>
$ screen -X -S [session you want to <span class="hljs-built_in">kill</span>] quit <span class="hljs-comment">#删除screen,指定会话</span>
$ screen -wipe <span class="hljs-comment"># 清除dead screens</span></code></pre>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E5%90%8E%E5%8F%B0/">后台</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Node/">Node</a>
                    
                      <a class="hover-with-bg" href="/tags/Mongodb/">Mongodb</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/12/05/%E6%97%A0%E7%BC%9D%E8%BD%AE%E6%92%AD%E5%9B%BE/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">无缝轮播图</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/11/23/Node+Express+Mongodb%E7%9A%84%E5%90%8E%E5%8F%B0%E7%B3%BB%E7%BB%9F/">
                        <span class="hidden-mobile">Node+Express+Mongodb的后台系统</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":200})
    NProgress.start()
    document.addEventListener('DOMContentLoaded', function() {
      window.NProgress && window.NProgress.inc();
    })
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.5.3/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script  src="https://cdn.staticfile.org/tocbot/4.12.0/tocbot.min.js" ></script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
